<script lang="ts" >
import { ref, defineComponent } from "vue";
import { Plus } from "@element-plus/icons-vue";
import ImgCutter from "@components/ImgCutter.vue";
import type { UploadProps, UploadUserFile } from "element-plus";
import { ElMessage, ElMessageBox } from "element-plus";
import videoSearch from "./hooks/videoSearch";
import videoForm from "./hooks/videoForm";
import { fileServiceUrl } from "@config";
import { RefreshRight, Delete } from "@element-plus/icons-vue";
const input = ref("");
export default defineComponent({
  name: "VideoList",
  components: {
    ImgCutter,
  },
  setup() {
    // 默认加载数据
    videoSearch().getVideoDataList();
    return {
      ...videoSearch(),
      // 这里将代码全部分离到ts模块中
      ...videoForm(),
      fileServiceUrl,
    };
  },
  computed: {
    VideoPicUrl() {
      let avatar = videoForm().videoForm.value.url ?? "";
      console.log("视频封面" + avatar);

      if (avatar.startsWith("http")) {
        return avatar;
      } else {
        if (avatar == null || avatar === "") {
          //ElMessage.warning(teacherForm().teacherForm.value.name + "头像信息加载失败！")
          return fileServiceUrl + avatar + "null.null";
        }
        return fileServiceUrl + avatar;
      }
    },
  },
});
</script>

<template >
  <div class="role-container">
    <div class="search-box">
      <el-form ref="searchFormRef" :model="searchForm" label-suffix=":" inline>
        <el-form-item label="视频标题" prop="title">
          <el-input v-model="searchForm.title"></el-input>
        </el-form-item>
        <el-form-item label="添加时间" prop="createTimeRange">
          <el-date-picker
          v-model="searchForm.createTimeRange"
            type="daterange"
            format="YYYY-MM-DD"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearchForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-form-item>
      <el-button @click="videoUpload">上传视频</el-button>
    </el-form-item>
    <!-- 表格 -->
    <el-table
      style="font-size: 14px"
      v-loading="tableLoading"
      :data="fileList"
      size="small"
      border
      stripe
    >
      <el-table-column label="视频封面" prop="avatar" align="center">
        <template #default="scope">
          <el-avatar
            @click="videoRun(scope.row.videoOriginalName)"
            style="width: 130px; cursor: pointer;height:80px"
            shape="square"
            :size="100"
            :src="
              scope.row.url != null &&
              scope.row.url != '' &&
              scope.row.url.startsWith('http')
                ? scope.row.url
                : fileServiceUrl + scope.row.url
            "
          />
        </template>
      </el-table-column>
      <el-table-column
        label="视频标题"
        prop="title"
        align="center"
      ></el-table-column>
      <el-table-column
        label="课程ID"
        prop="courseId"
        align="center"
      ></el-table-column>
      <el-table-column
        label="章节ID"
        prop="chapterId"
        align="center"
      ></el-table-column>
      <el-table-column label="状态" prop="isFree" align="center">
        <!-- 插槽 -->
        <template #default="scope">{{
          scope.row.isFree === 0 ? "免费" : "收费"
        }}</template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
           <el-button type="warning" plain @click="editVideo(row.id)">修改</el-button>
        
          <el-button type="warning" plain @click="delVideo(row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-model="dialogVisible" width="60%">
      <!-- <el-tag
        style="
          width: 60%;
          height: 40px;
          margin-left: 20%;
          font-size: 24px;
          margin-bottom: 5px;
        "
        v-text="currentVideo"
        ></el-tag
      > -->
      <video
        width="860"
        controls="controls"
        autoplay="autoplay"
        :src="fileServiceUrl+currentVideo"
      ></video>
    </el-dialog>
    <!-- 新增/修改 对话框 -->
    <el-dialog
      v-model="uploadVisible"
      width="600px"
      title="上传视频"
      destroy-on-close
      :close-on-click-modal="false"
      @close="resetVideoForm"
    >
      <el-form
        ref="videoFormRef"
        :model="videoForm"
        :rules="rules"
        label-suffix=""
        label-width="100px"
      >
        <!-- rules 配合 prop 实现数据校验 -->
        <el-form-item label="视频标题" prop="title">
          <el-input v-model="videoForm.title"></el-input>
        </el-form-item>
      
        <el-form-item label="视频状态" prop="isFree">
          <el-select v-model="videoForm.isFree" clearable placeholder="请选择">
            <el-option :value="1" label="收费" />
            <el-option :value="0" label="免费" />
          </el-select>
        </el-form-item>

        <el-form-item label="图片封面" align="center">
          <img-cutter
            :cut-width="100"
            :cut-height="70"
            :size-change="true"
            @cutDown="cutDown"
          >
            <template #open>
              <el-avatar
                shape="square"
                style="width: 80px; height: 50px"
                :src="VideoPicUrl"
              />
            </template>
          </img-cutter>
        </el-form-item>
        <el-form-item label="视频文件" prop="intro">
          <el-upload
            class="upload-demo"
            :http-request="videoFileUpload"
          >
            <el-button type="primary">选择视频</el-button>
          </el-upload>
          <!-- <el-input
            v-model="videoForm.videoOriginalName"
            :rows="5"
            type="file"
            @change="videoFileUpload"
          ></el-input> -->
        </el-form-item>
          <el-form-item label="课程ID" prop="courseId">
          <el-input v-model="videoForm.courseId"></el-input>
        </el-form-item>
        <el-form-item label="章节ID" prop="chapterId">
          <el-input v-model="videoForm.chapterId"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="videoForm.sort"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button type="danger" @click="uploadVisible = false">取消</el-button>
      </template>
    </el-dialog>

    <div class="page-wrapper">
      <el-pagination
        v-model:currentPage="searchForm.page"
        v-model:page-size="searchForm.pageSize"
        :page-sizes="[5, 10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        v-bind:total="total"
        @current-change="handleCurrentPageChange"
        @size-change="handlePageSizeChange"
      />
    </div>
  </div>
</template>

 



<style lang="scss" scoped>
.role-container {
  padding: 5px;
}
</style>